/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.abilities;

import static com.cmp.core.constants.InputUIDConstants.*;
import com.cmp.player.controls.CharacterRigidBodyControl;
import com.cmp.player.controls.CursorSelectionControl;
import com.cmp.player.controls.HealthControl;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * Normal attack that slash the opponent.
 *
 * @author krummer
 */
public class AttackControl extends AbilityControl {

    private boolean isAttack;
    private CharacterRigidBodyControl body;

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        super.setCooldown(0.f);
        super.setCastDuration(0.2f);

        spatial.setUserData(ATTACK_BUTTON_UID, false);
        this.body = spatial.getControl(CharacterRigidBodyControl.class);
    }

    @Override
    public void execute() {
        //only do when selecting a player.
        Node selected = super.getSpatial().getControl(CursorSelectionControl.class).getSelection();
        if (selected == null || selected.getUserData("isPlayer") == null) {
            return;
        }
        if (!this.isOnCooldown()) {

            super.activateCooldown();
            super.activateCastTime();


            //apply damage to target.
            HealthControl targetHP = selected.getControl(HealthControl.class);
            targetHP.removeHealth(25.f);
        }
    }

    @Override
    protected void controlUpdate(float tpf) {
        isAttack = super.getSpatial().getUserData(ATTACK_BUTTON_UID);
        if (isAttack) {
            execute();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
